package com.ezops.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ezops.dto.CashUpcomingPayRecieveModel;
import com.ezops.dto.ProductByCpModel;

/**
 * Helper class to controller for generating / manipulating the report data in
 * the required format of UI.
 * 
 * @author vgaurav
 * 
 */
@SuppressWarnings("unused")
public class ReportDataHelper {

	private static Logger logger = LoggerFactory
			.getLogger(ReportDataHelper.class);

	public static final String REPORT_PRODUCT_BY_CP_CSV =  "PositionbyCounterparty.csv";

	public static final String REPORTS_FOLDER_PATH = System
			.getProperty("user.home")
			+ System.getProperty("file.separator")
			+ "EZOPS"
			+ System.getProperty("file.separator")
			+ "Reports"
			+ System.getProperty("file.separator");

	private ReportDataHelper() {

	}

	/**
	 * Convert Map values in percentage for Position Charts
	 * 
	 * @param responseMesage
	 * @return
	 */
	public static Map<String, Double> convertMapValuesInPercentage(
			Map<String, Double> responseMesage) {
		double addValue = 0;
		try {
			for (Map.Entry<String, Double> entry : responseMesage.entrySet()) {
				addValue = addValue + entry.getValue();
			}
			Double percentageValue = 0.0;
			for (Map.Entry<String, Double> entry : responseMesage.entrySet()) {
				percentageValue = (entry.getValue() / addValue) * 100;
				responseMesage.put(entry.getKey(), percentageValue);
			}
		} catch (Exception e) {
			logger.error("Error ocurred while converting map values in percentage"
					+ e.getMessage());
		}
		return responseMesage;
	}

	/**
	 * Convert Map of position charts in list to return required JSON format for
	 * UI
	 * 
	 * @param productByCpModel
	 * @return
	 */
	public static List<ProductByCpModel> convertMapToList(
			Map<String, Double> responseMesage) {
		List<ProductByCpModel> productByCpModel = new ArrayList<>();
		try {
			for (Map.Entry<String, Double> entry : responseMesage.entrySet()) {
				ProductByCpModel obj = new ProductByCpModel();
				obj.setPartyId(entry.getKey());
				obj.setQuantity(String.valueOf(entry.getValue()));
				productByCpModel.add(obj);
			}
		} catch (Exception e) {
			logger.error("Error ocurred while converting position charts grid data into list"
					+ e.getMessage());
		}
		return productByCpModel;
	}

	/**
	 * Convert Map of Cash Upcoming Pay Receive charts in list to return
	 * required JSON format for UI
	 * 
	 * @param responseMesage
	 * @return
	 */
	public static List<CashUpcomingPayRecieveModel> convertMapToList1(
			Map<String, Double> responseMesage) {
		List<CashUpcomingPayRecieveModel> CashUpcomingPayRecieveModelList = new ArrayList<>();
		try {
			for (Entry<String, Double> entry : responseMesage.entrySet()) {
				CashUpcomingPayRecieveModel obj = new CashUpcomingPayRecieveModel();
				obj.setValueDate(entry.getKey());
				obj.setCashAmount(entry.getValue());
				CashUpcomingPayRecieveModelList.add(obj);
			}
		} catch (Exception e) {
			logger.error("Error ocurred while converting Cash Upcoming Pay Receive charts grid data into list"
					+ e.getMessage());
		}
		return CashUpcomingPayRecieveModelList;
	}
	
	/**
	 * 
	 * @param partyIdList
	 * @param agreementTypeList
	 * @return
	 */
	public static Map<String, List<Integer>> getMasterAggrementMapData(
			List<String> partyIdList, List<String> agreementTypeList) {
		Set<String> agreementSet = new HashSet<String>(agreementTypeList);
		Map<String, List<Integer>> responseMessage = new HashMap<String, List<Integer>>();

		for (String agreementKey : agreementSet) {
			List<Integer> temp = getList(agreementKey, partyIdList,
					agreementTypeList);
			responseMessage.put(agreementKey, temp);
		}
		return responseMessage;
	}
	
	/**
	 * 
	 * @param key
	 * @param partyIdList
	 * @param agreementTypeList
	 * @return
	 */
	private static List<Integer> getList(String key, List<String> partyIdList,
			List<String> agreementTypeList) {
		List<Integer> tempList = new ArrayList<>();
		for (int index = 0; index < agreementTypeList.size(); index++) {
			if (agreementTypeList.get(index).equalsIgnoreCase(key))
				tempList.add(1);
			else
				tempList.add(0);
		}
		return tempList;
	}

}
